/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package metodo;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Administrador
 */
public class metodospaciente {
   public Connection conexion;

    public boolean conectar() {

        try {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            String BaseDeDatos = "jdbc:oracle:thin:@192.168.0.38:1521:XE";
            conexion = DriverManager.getConnection(BaseDeDatos, "PROYECTO_NUTRI", "proyecto");
            if (conexion != null) {
                System.out.println("Conexion exitosa a esquema BASE");
                return true;
            } else {
                System.out.println("Conexion fallida");
            }
        } catch (Exception e) {
            System.out.println("error: " + e);
        }
        return false;
    }
    //----------------------------------------------------Muestra historya----------------------------------------
     public ResultSet muestra_historia(int X)
    {
    ResultSet rs=null;
    try{
        String sql="select altura, peso, imc, fecha  from historia  where idusuario like '" + X + "'";
        if (conectar()){
            Statement stt=conexion.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs=stt.executeQuery(sql);
            }
                return rs;
        }
    catch(Exception e){
        System.out.print("Error al consultar: " + e);
    }
    return rs;
    }


//----------------------------------------------CONSULTA PACIENTE----------------------------------------------------------------------------------------------

public ResultSet consultar(int x) {

        ResultSet rs = null;

        try {
            String sql = "select * from usuario inner join nutriologo_paciente on (id_usuario=id_paciente) where id_nutriologo="+x+"";
            if (conectar()) {
                Statement stt = conexion.createStatement();
                rs = stt.executeQuery(sql);
            }
            return rs;
        } 
       catch (Exception e) {
            System.out.print("Error al consultar: " + e);
        }
        return rs;
    }
//-------------------------------------------actuliza historia---------------------------------------

public String update_historia(int id, double alt, int pes) {

        
    try{
        if(conectar()){CallableStatement cs=null;
            cs=conexion.prepareCall("BEGIN ALTA_HISTORIA(?,?,?); END;");
    
    cs.setDouble(2, alt);
    cs.setInt(1, pes);
    cs.setInt(3, id);
    
   //Ejecutamos el procedimeinto
    cs.execute();
    
    conexion.close();
        }
            return "Registro Borrado";
        } 
    catch (Exception e) {
            return "Error al ejecutar el procedimiento: " + e;
        }
}
//--------------------------------------------------consulta dietas--------------------------------------------
public ResultSet consulta_dietas(int X)
    {
    ResultSet rs=null;
    try{
        String sql="select * from dieta inner join dieta_usuario on(dieta.ID_DIETA=DIETA_USUARIO.id_dieta) where DIETA_USUARIO.id_usuario=" + X + "";
        if (conectar()){
            Statement stt=conexion.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs=stt.executeQuery(sql);
            }
                return rs;
        }
    catch(Exception e){
        System.out.print("Error al consultar: " + e);
    }
    return rs;
    }
}
